import {Injectable} from '@angular/core';
import {ToastController} from "ionic-angular";
import {Network} from "@ionic-native/network";

@Injectable()
export class NetworkService {
    private modal: HTMLElement;
    constructor(public network: Network) {
        this.modal = null;
    }
    public init(element: HTMLElement) {
        if (typeof element === "undefined" || element == null) {
            return;
        }
        this.modal = element;
        this.modal.hidden = true;
    }
    /**
     * 检查网络
     */
    checkNetWork() {
        let net_type = this.network.type;
        if(net_type == "none"){
            this.showNetWorkError();
        }else{
            this.hideNetWorkError();
        }
    }

    /**
     * 显示网络异常
     */
    showNetWorkError(){
        if(this.modal.hidden){
            this.modal.hidden=false;
        }
        console.log("net work error...");
        this.modal.parentElement.style.zIndex="100000";
    }
    /**
     * 隐藏网络异常
     */
    hideNetWorkError(){
        if(!this.modal.hidden){
            this.modal.hidden=true;
        }
        this.modal.parentElement.style.zIndex="0";
    }
}
